package com.example.anni.zhuzhoujia.xiaowan;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.anni.zhuzhoujia.R;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;
import java.util.Map;

/**
 * Created by Anni on 2016/7/8.
 */
public class MyCommentAdapter extends RecyclerView.Adapter<MyViewHold4> {
    private List<Map<String, String>> mData;
    private Context mContext;
    private LayoutInflater mflater;
    private OnItemClickListener mOnItemClickListener;

    public MyCommentAdapter(Context mContext, List<Map<String, String>> mData) {
        this.mContext = mContext;
        this.mData = mData;
        mflater = LayoutInflater.from(mContext);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public MyViewHold4 onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mflater.inflate(R.layout.my_comment_adapter, parent, false);
        MyViewHold4 viewHold4 = new MyViewHold4(view);
        return viewHold4;
    }

    @Override
    public void onBindViewHolder(final MyViewHold4 holder, int position) {
        //设置点击时回调
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int layoutPosition = holder.getLayoutPosition();
                    mOnItemClickListener.OnItemClick(holder.itemView, layoutPosition);
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int layoutPosition = holder.getLayoutPosition();
                    mOnItemClickListener.OnItemLongClick(holder.itemView, layoutPosition);
                    return false;
                }
            });
        }
        //设置评论用户头像
        if (mData.get(position).get("avatar").length() == 0 && mData.get(position).get("avatar").equals("")) {
            holder.ivMyCommentHead.setImageResource(R.mipmap.null_head_image);
        } else {
            ImageOptions options = new ImageOptions.Builder()
                    .setRadius(10)
                    .setSquare(true)
                    .setCircular(true)
                    .setFailureDrawableId(R.mipmap.header_whilte)
                    .build();
            x.image().bind(holder.ivMyCommentHead,
                    "http://tr.zzapi.gson.cn/" + mData.get(position).get("avatar"),
                    options
            );
        }
        //设置昵称
        if (mData.get(position).get("nickName") != null) {
            holder.tvMyCommentNickName.setText(mData.get(position).get("nickName"));
        } else {
            holder.tvMyCommentNickName.setText("未设置");
        }

        //设置新闻标题
        String newsTitle = "{我在    }" + mData.get(position).get("newsTitle") + "{   中评论}";
        CharSequence chars = ColorPhrase.from(newsTitle)
                .withSeparator("{}").innerColor(0xFF000000).outerColor(0xFF4675FF).format();
        holder.tvMyCommentNewsTitle.setText(chars);

        //设置评论内容
        holder.tvMyCommentContent.setText(mData.get(position).get("content"));

        //设置评论时间
        holder.tvMyCommentDateline.setText(mData.get(position).get("dateline"));
        holder.tvMyCommentNewsTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "点我", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }
}

class MyViewHold4 extends RecyclerView.ViewHolder {
    ImageView ivMyCommentHead;
    TextView tvMyCommentNickName;
    TextView tvMyCommentNewsTitle;
    TextView tvMyCommentContent;
    TextView tvMyCommentDateline;

    public MyViewHold4(View itemView) {
        super(itemView);
        ivMyCommentHead = (ImageView) itemView.findViewById(R.id.iv_my_comment_head);
        tvMyCommentNickName = (TextView) itemView.findViewById(R.id.tv_my_comment_nickName);
        tvMyCommentNewsTitle = (TextView) itemView.findViewById(R.id.tv_my_comment_newsTitle);
        tvMyCommentContent = (TextView) itemView.findViewById(R.id.tv_my_comment_content);
        tvMyCommentDateline = (TextView) itemView.findViewById(R.id.tv_my_comment_dateline);
    }
}
